//
//  AppDelegate.swift
//  新浪微博
//
//  Created by mac on 15/11/26.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
            let account = UserAccount.loadAccount()
        print(account)
        
        //注册通知
        registerNotification()
        
        //手动实力化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewController()
        window?.makeKeyAndVisible()
        return true
    }

    private func isNewVersion()->Bool{
        //1 获取应用当前版本
        let dict = NSBundle.mainBundle().infoDictionary!
        let currentNumber = (dict["CFBundleShortVersionString"] as! NSString).doubleValue
        //2 获取上一个版本
        let sandBoxKey = "sandBoxKey"
        let defaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = defaults.doubleForKey(sandBoxKey)
        //记录当前的系统版本
        defaults.setValue(currentNumber, forKey: sandBoxKey)
        //同步沙盒
        defaults.synchronize()
        //3 比较两个版本
        return currentNumber > lastVersion
    }
    private func defaultRootViewController()->UIViewController{
    //判断用户是否登录
        if UserAccountViewModel().userLogin{
            
        //判断是新版本
            if isNewVersion(){
              return NewFeatureViewController()
            }
            return WelcomeViewController()
        }
    //如果没有登录
        return MainViewController()
        
    }
    private func registerNotification(){
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
    }
    //析构方法
    deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    @objc private func switchRootVC(n: NSNotification){
        window?.rootViewController = n.object == nil ? MainViewController(): WelcomeViewController()
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

